package com.javatiaocao.myblog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javatiaocao.myblog.mapper.ArticleMapper;
import com.javatiaocao.myblog.mapper.CommentMapper;
import com.javatiaocao.myblog.mapper.UserMapper;
import com.javatiaocao.myblog.model.Article;
import com.javatiaocao.myblog.model.Comment;
import com.javatiaocao.myblog.model.LeaveMessage;
import com.javatiaocao.myblog.service.CommentService;
import com.javatiaocao.myblog.utils.DataMap;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ArticleMapper articleMapper;


    @Override
    public DataMap getNewComment(Integer rows, Integer pageNum) {
        HashMap<String, Object> dataMap = new HashMap<>();
        HashMap<String, Object> pageInfo = new HashMap<>();
        //分页插件开启
        PageHelper.startPage(pageNum, rows);

        //获取评论
        List<Comment> leaveMessageList = commentMapper.getNewComment();
        PageInfo<Comment> page = new PageInfo<>(leaveMessageList);

        //rows:data['data']['pageInfo']['pageSize'],//每页显示条数
        //pageNum:data['data']['pageInfo']['pageNum'],//当前所在页码
        //pages:data['data']['pageInfo']['pages'],//总页数
        //total:data['data']['pageInfo']['total'],//总记录数
        pageInfo.put("pageSize",page.getPageSize());
        pageInfo.put("pageNum",page.getPageNum());
        pageInfo.put("pages",page.getPages());
        pageInfo.put("total",page.getTotal());

        ArrayList<HashMap> result = new ArrayList<>();
        //data['result'],function (index,obj)
        //obj['articleId']，obj['id']，obj['articleTitle']，obj['commentDate']
        // obj['answerer'] obj['commentContent']
        for (Comment comment : leaveMessageList) {
            HashMap<String, Object> object = new HashMap<>();
            object.put("articleId",comment.getArticleId());
            object.put("id",comment.getId());
            Article article = articleMapper.getArticleDetailByArticleId(comment.getArticleId());
            if(article == null){
                object.put("articleTitle","已删除文章");
            }else {
                object.put("articleTitle",article.getArticleTitle());
            }
            object.put("commentDate",comment.getCommentDate());
            object.put("answerer",userMapper.selectUserById(comment.getAnswererId()).getUsername());
            object.put("commentContent",comment.getCommentContent());
            result.add(object);
        }
        dataMap.put("result",result);
        dataMap.put("pageInfo",pageInfo);

        return DataMap.success().setData(dataMap);
    }
}
